package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.PhotoGallery;
import com.weixing.mall.provider.model.dto.PhotoChgDto;
import com.weixing.mall.provider.model.dto.PhotoGalleryDto;
import com.weixing.mall.provider.model.query.PhotoGalleryQuery;
import com.weixing.mall.provider.service.IPhotoGalleryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName PhotoGalleryController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/8/9
 * @Version 1.0
 **/
@RestController
@RequestMapping("photoGallery")
@Api(value = "图片库APi", tags = "图片库")
public class PhotoGalleryController extends BaseController {

    @Autowired
    private IPhotoGalleryService photoGalleryService;

    @ApiOperation(value = "分页条件查询图片库列表")
    @GetMapping("")
    public Result list(PhotoGalleryQuery photoGalleryQuery){
        return ResultUtil.success(photoGalleryService.selectOfPage(initMpPage(),photoGalleryQuery));
    }

    @ApiOperation(value = "保存图片")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@Validated PhotoGalleryDto photoGalleryDto) {
   /*     try{
            PhotoGallery photoGallery=    photoGalleryService.getOne(new QueryWrapper<PhotoGallery>().eq("album_id",photoGalleryDto.getAlbumId()).eq("photo_name",photoGalleryDto.getPhotoName()));
            if(photoGallery!=null){
                return ResultUtil.error("该相册已经存在该文件名称");
            }
        }catch (Exception e){
            return ResultUtil.error("发生未知异常，请联系管理员");
        }*/

        photoGalleryService.save(photoGalleryDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "转移相册")
    @PostMapping("update")
    public Result update(@Validated PhotoChgDto photoChgDto) {
        photoGalleryService.update(photoChgDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片id", paramType = "path", dataType = "long")
    })
    @PostMapping("del/{id}")
    public Result del(@PathVariable long id) {
        return photoGalleryService.removeById(id)? ResultUtil.success() : ResultUtil.error();
    }
}
